<!-- 添加分区 -->
<template>
  <el-dialog :visible.sync="dialogVisible" :before-close="handleCloseBindWarnStandard" :close-on-click-modal="false" :title="title" width="600px" height="300px">
    <el-form v-loading="formLoading" ref="dialogForm" :model="dialogForm" :rules="rules" label-width="120px">
      <el-form-item label="所属单位" prop="corp_id">
        <el-select v-if="showTopMenu" :disabled="dialogType === '修改'" v-model="dialogForm.corp_id" clearable filterable allow-create default-first-option autocomplete="off" placeholder="请选择单位名称" @change="companyChange">
          <el-option v-for="item in companyList" :key="item.corp_id" :label="item.name" :value="item.corp_id" />
        </el-select>
        <el-input v-else v-model="dialogForm.name" class="el-disabled" readonly="readonly" placeholder="请输入分区名称" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="所属建筑群" prop="park_id">
        <el-select :disabled="dialogType === '修改'" v-model="dialogForm.park_id" placeholder="请选择所属建筑群" @change="changeSelectPark">
          <el-option v-for="item in parksList" :key="item.park_id" :label="item.name" :value="item.park_id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属建筑" prop="building_id">
        <el-select :disabled="dialogType === '修改'" v-model="dialogForm.building_id" placeholder="请选择所属建筑" @change="changeSelectBuild">
          <el-option v-for="item in buildList" :key="item.building_id" :label="item.name" :value="item.building_id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属楼层" prop="floor_id">
        <el-select :disabled="dialogType === '修改'" v-model="dialogForm.floor_id" placeholder="请选择所属楼层">
          <el-option v-for="item in floorList" :key="item.floor_id" :label="item.name" :value="item.floor_id" />
        </el-select>
      </el-form-item>
      <el-form-item label="分区名称" prop="name">
        <el-input v-model="dialogForm.name" placeholder="请输入分区名称" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="NFC标签" prop="nfc_code">
        <el-input v-model="dialogForm.nfc_code" placeholder="请输入NFC标签" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getSocialUnit } from '@/api/company'
import { partitionUpdate, partitionAdd } from '@/api/partition'
import { parksList, buildList, floorList } from '@/api/park'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    dialogForm: {
      type: Object,
      default: () => {
        return {}
      }
    },
    dialogType: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      title: '',
      companyList: [],
      parksList: [],
      buildList: [],
      floorList: [],
      requestFn: {},
      // dialogForm: {},
      // 添加form验证
      rules: {
        corp_id: [{
          required: true,
          message: '请选择单位',
          trigger: 'blur'
        }],
        park_id: [{
          required: true,
          message: '请选择建筑群',
          trigger: 'blur'
        }],
        building_id: [{
          required: true,
          message: '请选择建筑',
          trigger: 'blur'
        }],
        floor_id: [{
          required: true,
          message: '请选择层楼',
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: '请输入分区',
          trigger: 'blur'
        }],
        nfc_code: [{
          required: true,
          message: '请输入NFC标签',
          trigger: 'blur'
        }]
      },
      showTopMenu: false,
      formLoading: false
    }
  },
  watch: {
    dialogType(val) {
      this.init(val)
    }
  },
  created() {
    if (sessionStorage.getItem('corpType') && sessionStorage.getItem('corpType') === '2') {
      this.showTopMenu = false
    } else {
      this.showTopMenu = true
    }
    this.init(this.dialogType)
  },
  methods: {
    init(dialogType) {
      if (this.dialogType === '添加') {
        this.title = '添加分区'
        this.requestFn = partitionAdd
        this.getCompanyList()
      } else if (this.dialogType === '修改') {
        this.title = '修改分区'
        this.requestFn = partitionUpdate
        this.getCompanyList()
        if (this.dialogForm.corp_id) {
          this.getParksList()
        }
        if (this.dialogForm.park_id) {
          this.getBuildList()
        }
        if (this.dialogForm.building_id) {
          this.getFloorList()
        }
      } else if (this.dialogType === '绘制分区添加') {
        this.title = '添加'
        this.requestFn = partitionAdd
        this.getCompanyList()
        if (this.dialogForm.corp_id) {
          this.getParksList()
        }
        if (this.dialogForm.park_id) {
          this.getBuildList()
        }
        if (this.dialogForm.building_id) {
          this.getFloorList()
        }
      }
    },
    submitForm(val) {
      this.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          const that = this
          that.formLoading = true
          var params = {
            corp_id: that.dialogForm.corp_id,
            park_id: that.dialogForm.park_id,
            building_id: that.dialogForm.building_id,
            floor_id: that.dialogForm.floor_id,
            name: that.dialogForm.name,
            zone_id: that.dialogForm.zone_id,
            nfc_code: that.dialogForm.nfc_code
          }
          that.requestFn(params)
            .then((res) => {
              that.formLoading = false
              that.dialogVisible = false
              const data = res.data
              if (data.code === 40000) {
                that.$message.success(data.message)
                that.$emit('submitForm', that.dialogForm)
              } else {
                that.$message.warning(data.message)
              }
            })
            .catch((err) => {
              console.log(err)
              that.formLoading = false
            })
        }
      })
    },
    // 关闭弹窗
    handleCancel() {
      this.$emit('getCancel')
    },
    // 关闭弹窗，不可以删这个方法。不用这个方法点击遮罩层关闭会报错
    handleCloseBindWarnStandard() {
      this.$emit('closeBindWarnStandard')
    },
    handleAvatarSuccess(res, file) {
      this.dialogForm.logo = URL.createObjectURL(file.raw)
    },
    // 单位下拉框查询,
    getCompanyList() {
      const that = this
      getSocialUnit()
        .then(response => {
          if (response.data.code === 40000) {
            that.companyList = response.data.data
          }
        })
        .catch(() => {
          this.list = []
          this.total = 0
        })
    },
    // 单位下拉框
    companyChange(value) {
      if (value !== '') {
        this.dialogForm.park_id = ''
        this.dialogForm.building_id = ''
        this.dialogForm.floor_id = ''
        this.getParksList()
      }
    },
    // 获取建筑群列表
    getParksList() {
      const that = this
      var listQuery = {
        page: 1,
        offset: 1000,
        corp_id: that.dialogForm.corp_id
      }
      parksList(listQuery)
        .then(response => {
          if (response.data.code === 40000) {
            that.parksList = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].index = index
              that.parksList.push(array[index])
            })
            console.log(that.parksList)
          }
        })
        .catch(() => {
          this.parksList = []
        })
    },
    changeSelectPark() {
      this.getBuildList()
      this.dialogForm.building_id = ''
      this.dialogForm.floor_id = ''
    },
    // 获取建筑列表
    getBuildList() {
      const that = this
      var listQuery = {
        page: 1,
        offset: 1000,
        park_id: that.dialogForm.park_id
      }
      buildList(listQuery)
        .then(response => {
          if (response.data.code === 40000) {
            that.buildList = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].index = index
              that.buildList.push(array[index])
            })
          }
        })
        .catch(() => {
          this.buildList = []
        })
    },
    changeSelectBuild() {
      this.getFloorList()
      this.dialogForm.floor_id = ''
    },
    // 获取楼层列表
    getFloorList() {
      const that = this
      var listQuery = {
        page: 1,
        offset: 1000,
        building_id: that.dialogForm.building_id
      }
      floorList(listQuery)
        .then(response => {
          if (response.data.code === 40000) {
            that.floorList = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].index = index
              that.floorList.push(array[index])
            })
          }
        })
        .catch(() => {
          this.floorList = []
        })
    },
    // 重置数据
    resetFields() {
      const that = this
      that.$nextTick(() => {
        if (that.dialogType === '添加') {
          that.$refs['dialogForm'].resetFields()// 移除表单校验并初始化表单
        } else {
          that.$refs['dialogForm'].clearValidate()// 移除表单校验
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
